(function(angular){
	'use strict';
	angular.module('fs.waf').controller('cda.dataPatch.viewController',viewController);
	viewController.$inject = ['$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'dataPatchService'];
	function viewController ($scope, Constants, layoutConfig, validatorConfig, gridService, authService, dataPatchService){
		var ctrl = this, dataPatchInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.dataPatch = {};
		ctrl.dataPatch.dPatchParms = ctrl.dataPatch.dPatchParms || [];
		ctrl.dataPatch.patchTemplate = ctrl.dataPatch.patchTemplate || {};
		ctrl.parmValues = [];
		
		ctrl.init = function (){
			var params = {
					dpatchId : dataPatchInfo.dpatchId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.dPatchParms.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.dataPatch.dPatchParms';
			
			return authService.view(params).then(function(response){
				dataPatchService.initOptions(ctrl);
				
				ctrl.dataPatch = response.data || dataPatchInfo;
				ctrl.dataPatch.dPatchParms = ctrl.dataPatch.dPatchParms || [];
				ctrl.dataPatch.dPatchParmsCopy = ctrl.dataPatch.dPatchParms;
				ctrl.dataPatch.patchTemplate = ctrl.dataPatch.patchTemplate || {};
				ctrl.dataPatch.patchTemplate.dPatchTplParms = ctrl.dataPatch.patchTemplate.dPatchTplParms ||[];
				
				if (ctrl.dataPatch.patchTemplate.dPatchTplParms && (ctrl.dataPatch.patchTemplate.dPatchTplParms).length >0
						&& ctrl.dataPatch.dPatchParmsCopy && (ctrl.dataPatch.dPatchParmsCopy).length >0){
					angular.forEach (ctrl.dataPatch.patchTemplate.dPatchTplParms, function (value, key){
						angular.forEach(ctrl.dataPatch.dPatchParmsCopy, function (value2, key){
							if (value.dpatchParmId === value2.dpatchParmId){
								value.paramValue = value2.paramValue;
								
							}
						});
						ctrl.parmValues.push(value);
						ctrl.dataPatch.dPatchParms = ctrl.parmValues;
					});
				}
			},angular.noop());
		};		
		
	}	
})(angular);